<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;
class carouselController extends Controller
{
    //轮播添加
    public function index()
    {
    	return view('admin.carousel.list');
    }
     public function create(Request $request)
    {
       return view('admin.carousel.add');
    }

    //执行添加
    public function store(Request $request)
    {
        //表单验证
        $this -> validate($request,[
                'address'=> 'required',
            ],[
                'address.required'=>'地址不能为空',
            ]);
        //获取参数
        $data = $request->except(['_token']);
        //文件上传
        if($request->hasFile('profile')) {
            $data['profile'] = $this->upload($request);
        }
        //写入数据库
        $res = DB::table('carousels')->insert($data);
        if($res) {
            return redirect('/admin/carousel')->with('info','添加成功');
        }else{
            return back()->with('info','添加失败');
        }
    }

    //文件上传操作
   

    //用户的列表显示功能
    public function getList(Request $request)
    {
       $carousels = DB::table('carousels')->orderBy('id','desc')->where(function($query)use($request){
            //检测是否传递keyword
            $keyword = $request->input('keyword');
            if($keyword) {
                $query->where('address','like','%'.$keyword.'%');               
            }
        })->paginate(4);
        //分配数据 解析模板
        return view('admin.carousel.list', ['carousels'=>$carousels,'request'=>$request]);
    }

    //显示修改
     public function edit($id)
    {
    	$profile = DB::table('carousels')->where('id',$id)->first();
    	return view('admin.carousel.edit',['profile'=>$profile]);
    }

    //执行修改
    public function update($id,Request $request)
    {
    	// 获取除了指定字段，剩下的字段
    	$data = $request->except('_token','_method');
    	 //dd($data);
        if($request->hasFile('profile')) {
            //拼接文件名称
            $fileName = time().rand(1000000,9999999);
            //获取文件的后缀名
            $suffix = $request->file('profile')->getClientOriginalExtension();
            //文件夹的规则
            $dir = './uploads/'.date('Ymd');
            $request->file('profile')->move($dir, $fileName.'.'.$suffix);
            //拼接路径
            $data['profile'] = trim($dir.'/'.$fileName.'.'.$suffix,'.');
        } 
    	$row = DB::table('carousels')->where('id',$id)->update($data);
    	//dd($row);

    	if($row>0){
    		return redirect('/admin/carousel')->with('msg','修改成功');
    	}
    }

      //文件上传操作
    public function upload($request)
    {
      if($request->hasFile('profile')) {
            //拼接文件名称
            $fileName = time().rand(1000000,9999999);
            //获取文件的后缀名
            $suffix = $request->file('profile')->getClientOriginalExtension();
            //文件夹的规则
            $dir = './uploads/'.date('Ymd');
            $request->file('profile')->move($dir, $fileName.'.'.$suffix);
            //拼接路径
            $data['profile'] = trim($dir.'/'.$fileName.'.'.$suffix,'.');
            return $data['profile'];
        }
        return null;  
    }

    // 删除
    public function destroy($id)
    {
        $row = DB::table('carousels')->where('id',$id)->delete();
        // dd($row);
        if($row>0){
            return redirect('/admin/carousel/')->with('msg','删除成功');
        }
    }


}
